class XMLTools
{
/*
:: DISCLAIMER ::
	This was written to handle a specific need of mine while writing a flex app for my employer.  Basically I call in an XML packet via a Web
Service and model the result.  Then I proceed to manipulate my newly created Object within my app. After that, I want to send this final Object
to a function that will convert it to an XML string so I can send it along its way to a waiting Web Service to handle the result.

	I would like to acknowledge James Polanco since I snagged alot of the code from the XMLObjectOutput.as file that 
Macromedia provides.  

	** Please note, I am new at actionscript so if you see ways to do things better in this script please fix it and let me know so I can update
mine as well.  Thanks.

------

individual attempting to write this code: Michael J. Prichard
email: michael.prichard@gmail.com
date: 07/09/2004
version: 1.0

function: toXML()
parameters: 
	1) object2Convert (* required)
			This is the Object in question.  Please pass this one.

function: loopObject()
parameters: 
	1) object2Convert (* required)
			This is the Object in question.  Please pass this one.
	2) arrayName (optional)
			I needed this to pass the name of the Array in question when looping through its elements.  I am assuming that the Array 
			is an Array of Objects and hence need to know parent node name that the elements are nested under.

*/

private var XMLPacket:String;
	
	// the constructor
	function XMLTools() {}

	function toXML(object2Convert:Object):String
	{
		XMLPacket="";
		loopObject(object2Convert);
		return XMLPacket;
		trace("here it is: " + XMLPacket);
	}

	private function loopObject(object2Convert:Object, arrayName:String):String
	{
		// is this Object an Array?  If so, we must handle differently
		if(object2Convert instanceof Array)
		{
			// walk the array
			for(var i=0; i < object2Convert.length; i++)
			{
				switch(true)
				{
					case(object2Convert[i] instanceof Object):
						// this arrayName is passed from below.  I need it to know the opening and closing tag names
						XMLPacket=XMLPacket + "<" + arrayName + ">";
						loopObject(object2Convert[i]);
						XMLPacket=XMLPacket + "</" + arrayName + ">";
						break;
					default:
						trace("|-- ERROR: SHOULD NOT GO HERE.");
				}	
			}
		// OK it is not an array.  lets run it through a switch.
		} else {
			// ok run through the Object	
			for(var i in object2Convert)
			{
				switch(true)
				{
				// again check for an array.  they are sneaky this way
				case(object2Convert[i] instanceof Array):
					loopObject(object2Convert[i], i);
					break;
				// if it is an object then run through that
				case(object2Convert[i] instanceof Object):
					// this is the opening tag
					XMLPacket=XMLPacket + "<" + i + ">";
						// ** this is an issue for me.  Sometimes the value of a parameter is viewed as an Object.  SO I toString() it to see.
						if (object2Convert[i].toString() == "[object Object]")
						{
							loopObject(object2Convert[i]);
						} else {
							XMLPacket=XMLPacket + "<![CDATA[" + object2Convert[i] + "]" + "]>";
						}
					// this is the closing tag...hehe I could not resist
					XMLPacket=XMLPacket + "</" + i + ">";
					break;
				default:
					// for the rest do this....means they are not showing up as Arrays or Objects
					XMLPacket=XMLPacket + "<" + i + ">";
					XMLPacket=XMLPacket + "<![CDATA[" + object2Convert[i] + "]" + "]>";
					XMLPacket=XMLPacket + "</" + i + ">";
				}
			}
		}
		// return the final XML String
		return XMLPacket;
	}
}